package com.hehe.boot.controller;

import com.hehe.boot.pojo.Diagnostic;
import com.hehe.boot.pojo.DiagnosticHttp.*;
import com.hehe.boot.pojo.Result;
import com.hehe.boot.service.DiagnosticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/diagnostic")
public class DiagnosticController {

    @Autowired
    private DiagnosticService diagnosticService;

    @PostMapping("/userAddDiagnostic")
    public Result<DiagnosticUserAddResponse> UserAdd(@RequestBody DiagnosticUserAddRequest request){
        Long id = diagnosticService.userAdd(request.getReservationId(),request.getDoctorId(), request.getUserId(),request.getTrouble());
        if (id == -1){
            return Result.error("UserAddDiagnostic error: fail to create Diagnostic");
        }
        DiagnosticUserAddResponse response = new DiagnosticUserAddResponse();
        response.setDiagnosticId(id);
        return Result.success(response);
    }

    @PostMapping("/doctorAddDiagnostic")
    public Result<DiagnosticDoctorAddResponse> DoctorAdd(@RequestBody DiagnosticDoctorAddRequest request){
        Long id = diagnosticService.doctorAdd(request.getDiagnosticId(),request.getDoctorId(),request.getSolution(),request.getDrugTerms());
        if (id == -1){
            return Result.error("DoctorAddDiagnostic error: fail to update Diagnostic");
        }
        if (id == -2){
            return Result.error("DoctorAddDiagnostic error: fail to update DiagnosticDrug");
        }

        DiagnosticDoctorAddResponse response = new DiagnosticDoctorAddResponse();
        response.setDiagnosticId(id);
        return Result.success(response);
    }

    @GetMapping("/show")
    public Result<DiagnosticShowResponse> Show(@RequestParam Long id){
        DiagnosticShowResponse response = diagnosticService.show(id);
        return Result.success(response);
    }

    @PostMapping("/userList")
    public Result<DiagnosticUserListResponse> UserList(@RequestBody DiagnosticUserListRequest request){
        DiagnosticUserListResponse response = diagnosticService.userList(request.getUserId());
        return Result.success(response);
    }

    @PostMapping("/doctorList")
    public Result<DiagnosticDoctorListResponse> DoctorList(@RequestBody DiagnosticDoctorListRequest request){
        DiagnosticDoctorListResponse response = diagnosticService.doctorList(request.getDoctorId());
        return Result.success(response);
    }

    @GetMapping("/userHistory")
    public Result<List<DiagnosticShowResponse>> UserHistory(@RequestParam Long id){
        return Result.success(diagnosticService.userHistory(id));
    }

    @GetMapping("/doctorHistory")
    public Result<List<DiagnosticShowResponse>> DoctorHistory(@RequestParam Long id){
        return Result.success(diagnosticService.doctorHistory(id));
    }

    @GetMapping("/getByReservationId")
    public Result<DiagnosticGetByReservationIdResponse> GetByReservationId(@RequestParam Long reservationId){
        return Result.success(diagnosticService.getByReservationId(reservationId));
    }
}
